//
//  AppDelegate.m
//  SITABLE
//
//  Created by songjun on 16/9/6.
//  Copyright © 2016年 Sita. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    

  
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginNoty:) name:@"loginNoty" object:nil];
    
     NSString *userLogOff = [[NSUserDefaults standardUserDefaults] valueForKey:@"LinBoardIsLogOff"];
    
    NSString *isfirstLog = [[NSUserDefaults standardUserDefaults] valueForKey:@"LinBoardIsfirstLog"];

    if (isfirstLog.length == 0) {
        if (!loginVc) {
            loginVc=[[UIStoryboard storyboardWithName:@"LoginRegisterSb" bundle:nil] instantiateViewControllerWithIdentifier:@"loginVc"];
            UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
            self.window.rootViewController = loninNc;
        }else{
            UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
            self.window.rootViewController = loninNc;
        }
       
        
    }else{
        if ([userLogOff isEqualToString:@"yes"]) {
            if (!loginVc) {
                loginVc=[[UIStoryboard storyboardWithName:@"LoginRegisterSb" bundle:nil] instantiateViewControllerWithIdentifier:@"loginVc"];
                UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
                self.window.rootViewController = loninNc;
            }else{
                UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
                self.window.rootViewController = loninNc;
            }

        }else{
            if (!mainVc) {
                mainVc =[[PallaTabarViewController alloc] init];
                self.window.rootViewController = mainVc;
            }else{
               self.window.rootViewController = mainVc;
            }
        }
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOff) name:@"logOff" object:nil];
    [self configureAPIKey];
    [self setNav];
    return YES;
}


-(void)logOff
{

    
//    LoginViewController *loginVc = [[UIStoryboard storyboardWithName:@"LoginRegisterSb" bundle:nil] instantiateViewControllerWithIdentifier:@"loginVc"];
//    UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
//    self.window.rootViewController = loninNc;
    
    if (!loginVc) {
        loginVc=[[UIStoryboard storyboardWithName:@"LoginRegisterSb" bundle:nil] instantiateViewControllerWithIdentifier:@"loginVc"];
        UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
        self.window.rootViewController = loninNc;
    }else{
        UINavigationController *loninNc = [[UINavigationController alloc] initWithRootViewController:loginVc];
        self.window.rootViewController = loninNc;
    }

    
    
    [Utilization destroyLKDBHelper];
    [[NSUserDefaults standardUserDefaults] setValue:@"yes" forKey:@"LinBoardIsLogOff"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    


}


-(void)loginNoty:(NSNotification *)noty
{
    [[NSUserDefaults standardUserDefaults] setValue:@"no" forKey:@"LinBoardIsfirstLog"];
    [[NSUserDefaults standardUserDefaults] synchronize];
//    PallaTabarViewController *yaDeaTabVc = [[PallaTabarViewController alloc] init];
//    self.window.rootViewController = yaDeaTabVc;
    
    if (!mainVc) {
        mainVc =[[PallaTabarViewController alloc] init];
        self.window.rootViewController = mainVc;
    }else{
        self.window.rootViewController = mainVc;
    }

}



#pragma mark ----初始化navigationbar----
- (void)setNav
{
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.translucent = NO;
    bar.barTintColor = UIColorHexRGBA(linBoardNavitagionColor, 1.0);
    //设置字体颜色
    bar.tintColor = [UIColor whiteColor];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    //去掉黑线
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [[UINavigationBar appearance] setTranslucent:NO];
}


#pragma mark ----初始化高德地图的key----
- (void)configureAPIKey
{
    [AMapServices sharedServices].apiKey = linBoardMapkey;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"toConnectBleNoty" object:nil];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
